//
//  StepHeader.m
//  ChampoinPeso
//
//  Created by mac on 2025/2/16.
//

#import "StepHeader.h"
@interface StepHeader()
@property (nonatomic , strong)UIView *lineView;
@property (nonatomic,strong)UIButton *temp;
@end
@implementation StepHeader

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}
-(void)setupUI{
    NSArray *titles = @[@"Recommended  \n ID Type",@"Other options"];
    CGFloat width = (SCREEN_WIDTH - 26)/2;
    [titles enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        @autoreleasepool{
            UIView *view = [UIView new];
            [self addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset((SCREEN_WIDTH - 26)/2 *idx);
                make.top.mas_offset(0);
                make.width.mas_equalTo(width);
                make.bottom.mas_offset(0);
            }];
            UIButton *item = [UIButton buttonWithType:UIButtonTypeCustom];
            item.tag = idx ;
            [item setTitleColor:DetailColor forState:UIControlStateNormal];
            [item setTitleColor:MainColor forState:UIControlStateSelected];
            [item setTitle:obj forState:UIControlStateNormal];
            [item.titleLabel setFont:[UIFont systemFontOfSize:18 weight:UIFontWeightMedium]];
            item.titleLabel.adjustsFontSizeToFitWidth = YES;
            [item addTarget:self action:@selector(selectItem:) forControlEvents:UIControlEventTouchUpInside];
            if (idx == 0) {
                item.selected = YES;
                item.titleLabel.numberOfLines = 2;
                item.titleLabel.textAlignment = NSTextAlignmentCenter;
                _temp = item;
            }
            [view addSubview:item];
            [item mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.mas_equalTo(view);
            }];
        }
    }];
    self.lineView = [[UIView alloc]initWithFrame:CGRectMake((width/2)-20, 80, 40, 5)];
    self.lineView.backgroundColor = MainColor;
    self.lineView.layer.cornerRadius = 2.5;
    [self addSubview:self.lineView];

}
-(void)selectItem:(UIButton*)sender
{
    if (sender == _temp) {
        return;
    }
    _temp.selected = NO;
    sender.selected = YES;
    _temp = sender;
    [self scrollToItemCenter:sender];
}
- (void)scrollToItemCenter:(UIButton *)item
{
    CGFloat width = (SCREEN_WIDTH - 26)/2;
    if (item.tag == 0) {
        [UIView animateWithDuration:0.15 animations:^{
            self.lineView.centerX = width/2;
        }];
    }else{
        [UIView animateWithDuration:0.15 animations:^{
            self.lineView.centerX = width/2 +width;
        }];
    }
}

@end
